package ${packageUri}.web.service.impl;

import com.zdww.cd.sxdv.web.vo.res.ServiceResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ${packageUri}.web.dao.${className}Dao;
import ${packageUri}.web.dto.${className}DTO;
import ${packageUri}.web.entity.${className};
import ${packageUri}.web.service.${className}Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName ${className}ServiceImpl
 * @Description ${tableDesc}
 * @Author duqc
 * @Date ${time}
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class ${className}ServiceImpl implements ${className}Service {

    private final ${className}Dao ${lowClassName}Dao;

    /**
     * 通过id查询详情信息
     *
     * @param  id 唯一ID
     * @return ${className}
     */
    @Override
    public ServiceResponse<${className}> getInfoById(String id) {
        if (StringUtils.isBlank(id)) {
            return ResultMsg.failure("参数错误");
        }
        return ServiceResponse.success(this.${lowClassName}Dao.getInfoById(id));
    }

    /**
    * 列表查询
    *
    * @param ${className?uncap_first}DTO 参数
    * @return PageInfo
    */
    @Override
    public ServiceResponse<PageInfo<${className}>> list(${className}DTO ${className?uncap_first}DTO) {
        PageHelper.startPage(${className?uncap_first}DTO.getPageNum(), ${className?uncap_first}DTO.getPageSize());
        return ServiceResponse.success(new PageInfo<>(this.${lowClassName}Dao.list()));
    }

    /**
     * 删除（逻辑删除）
     *
     * @param  id 唯一ID
     * @return ServiceResponse
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServiceResponse delete(Long id) {
        if (null == id) {
            return ServiceResponse.fail("参数错误");
        }
        ${className} ${className?uncap_first} = new ${className}();
        ${className?uncap_first}.setId(id);
        Integer count = this.${lowClassName}Dao.delete(${className?uncap_first});
        if (count > 0) {
            return ServiceResponse.successful();
        }
        return ServiceResponse.fail();
    }

    /**
    * 保存
    *
    * @param  ${className?uncap_first} 保存数据
    * @return ServiceResponse
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMsg insert(${className} ${className?uncap_first}) {
        if (null == ${className?uncap_first}) {
            return ServiceResponse.fail("参数错误");
        }
        Integer count = this.${lowClassName}Dao.insert(${className?uncap_first});
        if (count > 0) {
            return ServiceResponse.successful();
        }
        return ServiceResponse.fail();
    }

    /**
    * 修改
    *
    * @param  ${className?uncap_first} 修改数据
    * @return ServiceResponse
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMsg update(${className} ${className?uncap_first}) {
        if (null == ${className?uncap_first}) {
            return ServiceResponse.fail("参数错误");
        }
        if (null == ${className?uncap_first}.getId()) {
            return ServiceResponse.fail("参数错误");
        }
        Integer count = this.${lowClassName}Dao.update(${className?uncap_first});
        if (count > 0) {
            return ServiceResponse.successful();
        }
        return ServiceResponse.fail();
    }
}
